from Common.Driver import browser
from selenium.webdriver.common.by import By
from Common.OperatElement import OperationElement
from time import sleep

class OrderPage(OperationElement):
    # 自定义全局变量区

    # 页面元素路径区
    # ======元素开始区域======
    input_element = (By.XPATH, "(//{}[@{}='{}'])[{}]")
    input_element2 = (By.XPATH, "(//{}[contains(text(),'{}')])[{}]")
    country = (By.XPATH, "(//*[@id='country'])[1]/option")
    city = (By.XPATH, "(//*[@id='province'])[1]/option")
    area = (By.XPATH, "(//*[@id='city'])[1]/option")

    def page_send_keys(self, *args, data):
        return self.send_keys(self.input_element, *args, value=data)

    def page_click(self, *args):
        return self.click(self.input_element, *args)

    def page_click_(self, *args):
        return self.click(self.input_element2, *args)

    def get_contry(self):
        list_data = []
        country = self.search_elements(self.country)
        for value in country:
            list_data.append(((value.text).strip()))
        return list_data

    def get_city(self):
        list_data = []
        country = self.search_elements(self.city)
        for value in country:
            list_data.append(((value.text).strip()))
        return list_data

    def get_area(self):
        list_data = []
        try:
            country = self.search_elements(self.area)
            for value in country:
                list_data.append(((value.text).strip()))
            return list_data
        except:
            return list_data

class PlaceOrder(object):
    def __init__(self):
        self.driver = browser()
        self.url = "http://www.adorawe-pc.com.release.fpm.testsdlk.com/category/t-shirts-c_97.html"
        self.login_url = "http://user.adorawe-pc.com.release.fpm.testsdlk.com/user/login?ref=http://order.adorawe-pc.com.release.fpm.testsdlk.com/order/checkout"
        self.element = (By.XPATH, "(//*[@class='name'])[1]")
        self.e2 = (By.XPATH, "(//*[@class='header-nav-item js-header-nav-item js-header-nav-link'])[1]")

    def place_order(self):

        PageObj = OrderPage(self.driver)
        PageObj.implicit(30)
        PageObj.get(self.login_url)
        PageObj.page_send_keys('*', 'id', 'sign-email', 1, data='610974320@qq.com')
        PageObj.page_send_keys('*', 'id', 'sign-password', 1, data='cjb0107')
        PageObj.page_click('*', 'class', 'font-lato-bold js-sign-submit', 1)
        sleep(1)
        self.driver.get(self.url)
        sleep(1)
        PageObj.hovers(self.element)
        PageObj.page_click('*', 'class', 'quick-view', 1)
        sleep(1)
        PageObj.page_send_keys('*', 'class', 'numinput', 1, data='3')
        PageObj.page_click('*', 'id', 'new_addcart', 1)
        sleep(1)
        PageObj.page_click('*', 'class', 'minicart-checkout js_minicartCheckout', 1)
        sleep(1)
        PageObj.page_click( '*', 'class', 'js_checkoutOpenAddrEdition checkout-block-link', 1)
        sleep(1)
        PageObj.page_click('*', 'class', 'js_checkoutEditAddr', 1)
        sleep(2)
        country = PageObj.get_contry()
        PageObj.page_click_('*', country[1], 1)
        sleep(2)
        city = PageObj.get_city()
        PageObj.page_click_('*', city[1], 1)
        sleep(2)
        area = PageObj.get_area()
        if city:
            PageObj.page_click_('*', area[1], 1)
        else:
            PageObj.page_send_keys('*', 'id', 'city', 1, data='test')
        sleep(2)
        PageObj.page_click('*', 'data-mode', 'edit', 1)
        PageObj.page_click('*', 'class', 'checkedLabel', 1)
        PageObj.page_click('*', 'class', 'js_checkoutSelectShippingMethod', 1)
        PageObj.page_click('*', 'id', 'checkout_submit', 1)



